<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<% 
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>公告管理修改界面</title>
<link type="text/css" rel="stylesheet" href="<%=basePath %>jquery-easyui-1.4.2/themes/default/easyui.css">
<link type="text/css" rel="stylesheet" href="<%=basePath %>jquery-easyui-1.4.2/themes/icon.css">
<script type="text/javascript" src="<%=basePath %>js/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="<%=basePath %>jquery-easyui-1.4.2/jquery.easyui.min.js"></script>
<script type="text/javascript" src="<%=basePath %>jquery-easyui-1.4.2/locale/easyui-lang-zh_CN.js"></script>
<style type="text/css">
td{
		border:none;
	}
a{
	text-decoration: none;
	color: #81A1FF;
}
#menuname{
	width: 200px;
	height: 20px;
	margin-left: 100px;
	margin-top: 20px;
	border-radius: 5px;
}
#menuisShowDiv{
	width: 200px;
	height: 20px;
	margin-left: 100px;
	margin-top: 20px;
}
#checkDiv{
	margin-top: 20px;
}
#menunameUpdata{
	width: 200px;
	height: 20px;
	margin-left: 100px;
	margin-top: 20px;
	border-radius: 5px;
}
#menuisShowDivUpdata{
	width: 200px;
	height: 20px;
	margin-left: 100px;
	margin-top: 20px;
}
#checkDivUpdata{
	margin-top: 20px;
}
</style>
<script type="text/javascript">
var menuidDelete = -1;
var menuparentldDelete = -1;
	$(function () {
		//启动时关闭窗口
		$('#win').window('close');
		$('#winUpdata').window('close');
		$('#errorWin').window('close');
		//使用ajax再次访问以下数据
		<%-- location.href="<%=basePath %>bulletin/bulletinTreeMenuByUserId.do?userId=2&parentId=1"; --%>
		$.ajax({
			type:"post",
			url:"<%=basePath %>bulletin/bulletinMenuByUserId.do",
			data:{"userId":2,"parentId":1},
			dataType:"json",
			success:function(data){
				var json = eval(data);
				var i = 0;
				var c = 0;
				var roots = new Array();
				for (var d in json) {
					if (json[d].menuLevel == 2 && json[d].menuname !='公告管理设置') {
						$("#tableRoot").append("<tr class='trRoot'><td>"+json[d].menuid+"</td><td>"+json[d].menuname+"</td></tr>");
						if (json[d].menuisShow==1) {
							$(".trRoot").eq(i).append("<td>使用</td>");
						}
						if (json[d].menuisShow==0) {
							$(".trRoot").eq(i).append("<td>不使用</td>");
						}
						$(".trRoot").eq(i).append("<td style='color:#81A1FF'><a href='javascript:openWin(2,"+json[d].menuid+")' >添加子节点</a >&nbsp;<a href='javascript:openWinUpdata("+json[d].menuid+")'>修改</a>&nbsp;<a href='javascript:deleteWin("+json[d].menuid+","+json[d].menuparentld+")'>删除</a></td> ");
						i++;
						roots.push(json[d]);
					}
				}
				for(var d in json){
					for (var j = 0; j < roots.length; j++) {
						if (json[d].menuLevel == 3 && json[d].menuparentld == roots[j].menuid) {
							$("#tableChild").append("<tr class='trRootChild'><td>"+json[d].menuparentld+"</td><td>"+json[d].menuname+"</td></tr>");
							if (json[d].menuisShow==1) {
								$(".trRootChild").eq(c).append("<td>使用</td>");
							}
							if (json[d].menuisShow==0) {
								$(".trRootChild").eq(c).append("<td>不使用</td>");
							}
							$(".trRootChild").eq(c).append("<td style='color:#81A1FF'><a href='updataContent.jsp?menuname="+json[d].menuname+"' >添加修改内容</a >&nbsp;&nbsp;<a href='javascript:openWinUpdata("+json[d].menuid+")'>修改</a>&nbsp;<a href='javascript:deleteWin("+json[d].menuid+","+json[d].menuparentld+")'>删除</a></td> ");
							c++;
						}
						
					}
				}
				
				
			}
		})
	})
	
	//打开窗口
	function openWin(index,menuparentld) {
		$('#win').window('open');
		$('#win').panel({title:'添加'});
		if (index == 1) {
			$("#menuname").attr("placeholder","根节点名字");
		}
		if (index == 2) {
			$("#menuname").attr("placeholder","子节点名字");
			$("#menuparentld").val(menuparentld);
		}
				
	}
	
	//节点的增加修改
	function save() {
		var menuparentld = $("#menuparentld").val();
		if (menuparentld == "") {
			var menuid = -1;
			var menuLevel = 2;
			menuparentld = 1;
			var menuUrl = "";
		}else{
			var menuid = -1;
			var menuLevel = 3;
			var menuUrl = "bulletin/bulletin.jsp";
		}
		var menuname = $("#menuname").val();
		var menuisShow = $("#menuisShow").combobox("getValue");
		if (menuisShow == -1) {
			$.messager.alert('提示','请选择使用状态！','info');
			return;
		}
		//发送请求
		$.post("<%=basePath%>bulletin/saveOrUpdate.do",{
			'menuid':menuid,
			'menuname':menuname,
			'menuparentld':menuparentld,
			'menuUrl':menuUrl,
			'menuLevel':menuLevel,
			'menuisShow':menuisShow
		},function(data){
			if (data == "ok") {
				$.messager.alert('消息','添加成功','info',function(){
					close();
				});
			}
		},"json")
	}
	//打开修改窗口
	function openWinUpdata(menuid) {
		$('#winUpdata').window('open');
		$('#winUpdata').panel({title:'修改'});
		$.ajax({
			type:"post",
			url:"<%=basePath %>bulletin/menuBymenuid.do",
			data:{"menuid":menuid},
			dataType:"json",
			success:function(data){
				$('#menuidUpdata').val(data.menuid);
				$('#menunameUpdata').val(data.menuname);
				$('#menuisShowUpdata').combobox('setValue',data.menuisShow);
			}
		})
	}
	
	//修改
	function updata(){
		var menuid = $("#menuidUpdata").val();
		var menuname = $('#menunameUpdata').val();
		var menuisShow = $("#menuisShowUpdata").combobox("getValue"); 
		$.ajax({
			type:"post",
			url:"<%=basePath %>bulletin/updataMenu.do",
			data:{
				"menuid":menuid,
				"menuname":menuname,
				"menuisShow":menuisShow,
			},
			dataType:"json",
			success:function(data){
				if (data == "ok") {
					$.messager.alert('消息','修改成功','info',function(){
						close();
					});
				}
			}
		})
	}
	
	//关闭窗口
	function close() {
		location.reload();
	}
	
	//打开删除窗口
	function deleteWin(menuid,menuparentld) {
		$('#errorWin').window('open');
		if (menuparentld == 1) {
			$("#removep").text("该节点为根节点，删除该节点会删除它所有的子节点");
		}
		menuidDelete = menuid;
		menuparentldDelete = menuparentld;
	}
	//删除节点
	function removeMenu(){
		$.ajax({
			type:"post",
			url:"<%=basePath %>bulletin/removeMenu.do",
			data:{'menuidDelete':menuidDelete,'menuparentldDelete':menuparentldDelete},
			dataType:"json",
			success:function(data){
				if (data == "ok") {
					$.messager.alert('消息','删除成功,请点击右下角刷新','info',function(){
						location.reload();
					});
				}
			}
		})
	}
</script>
</head>
<body>
	<!-- 增加窗口 -->
	<div id="win" class="easyui-window"  style="width:400px;height:200px"   
        data-options="iconCls:'icon-add2',modal:true,collapsible:false,minimizable:false">  
   		<form action="" id="form">
   			<div>
   				<input type="hidden" class="easyui-validatebox input" id='menuparentld'/>
   			</div>
   			<div>
   				<input type='text' class="easyui-validatebox input" id='menuname'/>
   			</div>
   			<div id="menuisShowDiv">
   				<select id="menuisShow" name="menuisShow" class="easyui-combobox input" style="width: 205px;height: 25px">   
   					<option value="-1">请选择使用状态</option>
   					<option value="1">使用</option>
   					<option value="0">不使用</option>
   				</select>
   			</div>
   			<div style="text-align: center;" id="checkDiv">
   				<a  href="javascript:save();" class="easyui-linkbutton" data-options="iconCls:'icon-add2'">确定</a>
   				<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
   				<a  href="javascript:close();" class="easyui-linkbutton" data-options="iconCls:'icon-undo'">取消</a>  
   			</div>
   		</form>  
	</div>
	
	<!-- 修改窗口 -->
	<div id="winUpdata" class="easyui-window"  style="width:400px;height:200px"   
        data-options="iconCls:'icon-add2',modal:true,collapsible:false,minimizable:false">  
   		<form action="" id="formUpdata">
   			<div>
   				<input type="hidden" class="easyui-validatebox input" id='menuidUpdata'/>
   			</div>
   			<div>
   				<input type='text' class="easyui-validatebox input" id='menunameUpdata'/>
   			</div>
   			<div id="menuisShowDivUpdata">
   				<select id="menuisShowUpdata" name="menuisShow" class="easyui-combobox input" style="width: 205px;height: 25px">   
   					<option value="-1">请选择使用状态</option>
   					<option value="1">使用</option>
   					<option value="0">不使用</option>
   				</select>
   			</div>
   			<div style="text-align: center;" id="checkDivUpdata">
   				<a  href="javascript:updata();" class="easyui-linkbutton" data-options="iconCls:'icon-add2'">确定</a>
   				<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
   				<a  href="javascript:close();" class="easyui-linkbutton" data-options="iconCls:'icon-undo'">取消</a>  
   			</div>
   		</form>  
	</div>

	<!-- 删除窗口 -->
	<div id="errorWin" class="easyui-window" title="My Window" style="width:350px;height:170px;text-align: center;" 
        data-options="iconCls:'icon-add2',modal:true,collapsible:false,minimizable:false">
         <p class="removep">温馨提示：你确定要删除该段节点吗！</p>
         <p id="removep" class="removep"></p>
         <p class="removep">删除后将无法恢复！</p>     
        <div style="text-align: center;">
			<a  href="javascript:removeMenu();" class="easyui-linkbutton" data-options="iconCls:'icon-add2'">确定</a>
			<a  href="javascript:close();" class="easyui-linkbutton" data-options="iconCls:'icon-undo'">取消</a>  
		</div>
    </div>


	<input style="margin-left:5%;" type="button" value="添加根节点" onclick="javascript:openWin(1,-1)"/>
	<table id="tableRoot" cellspacing="0" cellpadding="0" align="center" style="width:90%;margin-top: 10px;text-align: center;" bordercolor="#A2C5E5">   
       	<tr style="background-color: #EDF5FB">   
            <td colspan="4" style="border-bottom:solid 1px #A2C5E5;color: #0E6CA4">根节点表</td>   
        </tr>
        <tr style="background-color: #EDF5FB">   
            <td style="border-bottom:solid 1px #A2C5E5;color: #0E6CA4" width="25%">根节点序号</td>   
            <td style="border-bottom:solid 1px #A2C5E5;color: #0E6CA4" width="25%">名称</td>   
            <td style="border-bottom:solid 1px #A2C5E5;color: #0E6CA4" width="25%">状态</td>
            <td style="border-bottom:solid 1px #A2C5E5;color: #0E6CA4" width="25%">操作</td>    
        </tr>  
        
	</table> 
	<table id="tableChild" cellspacing="0" cellpadding="0" align="center" style="width:90%;margin-top: 40px;text-align: center;" bordercolor="#A2C5E5">   
        <tr style="background-color: #EDF5FB">   
            <td colspan="4" style="border-bottom:solid 1px #A2C5E5;color: #0E6CA4">子节点表</td>   
        </tr>
        <tr style="background-color: #EDF5FB">   
       		<td style="border-bottom:solid 1px #A2C5E5;color: #0E6CA4" width="25%">父节点序号</td>  
            <td style="border-bottom:solid 1px #A2C5E5;color: #0E6CA4" width="25%">名称</td> 
            <td style="border-bottom:solid 1px #A2C5E5;color: #0E6CA4" width="25%">状态</td>
            <td style="border-bottom:solid 1px #A2C5E5;color: #0E6CA4" width="25%">操作</td>    
        </tr>  
       
	</table>   
	
</body>
</html>